package demo;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-07-19
 * Time: 19:24
 */
public class UpdateJDBC {


    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/ebook?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        //建立连接
        Connection connection = dataSource.getConnection();
        //构建sql
        /**
         * select id from book where name = '深入理解Java虚拟机';
         * +----+
         * | id |
         * +----+
         * |  1 |
         * +----+
         *
         * mysql> update book set book.price = 61.20 where id = 1;
         *
         * mysql> select * from book;
         * id | name        | author           | price  | category_id
         */

        String sql = "update book set price= 61.20 where name ='深入理解Java虚拟机';";

        PreparedStatement statement = connection.prepareStatement(sql);
        int n = statement.executeUpdate();
        System.out.println("n = " + n);

        //释放连接资源
        statement.close();
        connection.close();

    }

}
